/*
 * @Author: byAcya xiaoxianbin@4399inc.com
 * @Date: 2025-02-21 11:31:48
 * @LastEditors: byAcya xiaoxianbin@4399inc.com
 * @LastEditTime: 2025-02-21 11:54:20
 * @FilePath: \CocosFramework\assets\script\core\utils\TimeUtils.ts
 * @Description:
 */
import { Director, director } from 'cc';
import Instance from './Instance';

export interface ITimerUtils {
    init(): void;
    add(delay: number, repeat: number, callback: Function, target: Object): void;
    remove(callback: Function, target: Object): void;
}

interface ITimer {
    tick: number;
    interval: number;
    repeat: number;
    callback: Function;
    target: Object;
}
export class TimeUtils implements ITimerUtils {
    public static getInstance(): TimeUtils {
        return Instance.get<TimeUtils>(TimeUtils);
    }

    private timers: ITimer[] = [];

    init() {
        director.on(Director.EVENT_BEFORE_UPDATE, this.update, this);
    }

    add(delay: number, repeat: number, callback: Function, target: Object) {
        if (repeat == 0) return;
        this.remove(callback, target);
        const timer: ITimer = {
            tick: 0,
            interval: delay,
            repeat: repeat,
            callback: callback,
            target: target,
        };
        this.timers.push(timer);
    }

    remove(callback: Function, target: Object) {
        this.timers = this.timers.filter((timer) => {
            return timer.callback !== callback || timer.target !== target;
        });
    }

    update(dt: number) {}
}
